package com.bawei.module_home.ui.merchant

import android.app.DatePickerDialog
import android.content.Intent
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_common.view.FlowLayoutWithAddView
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityAddMerchantBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.AddMerchantBody
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

@AndroidEntryPoint
@Route(path = Router.MERCHANT_ADD_PAGE)
class AddMerchantActivity : BaseActivity<HomeServiceViewModel, ActivityAddMerchantBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_add_merchant
        val mViewModel: HomeServiceViewModel by viewModels()
    private val calendar = Calendar.getInstance()
    override fun initView() {
        super.initView()
        mBinding.toobar.apply {
            more.setText("申请记录")
            more.setOnClickListener({
                ARouter.getInstance().build(Router.MERCHANT_PAGE).navigation()
            })
            title.text="入驻申请"
            back.setOnClickListener({
                finish()
            })
        }
        updateDateInView()
        mBinding.time.setOnClickListener({
            showDatePickerDialog()
        })
    }
    private fun showDatePickerDialog() {
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog(
            this,
            { _, selectedYear, selectedMonth, selectedDay ->
                // 用户选择日期后的回调
                calendar.set(selectedYear, selectedMonth, selectedDay)
                updateDateInView()
            },
            year,
            month,
            day
        )

        // 可选：设置日期范围限制
        // datePickerDialog.datePicker.minDate = ...
        // datePickerDialog.datePicker.maxDate = ...

        datePickerDialog.show()
    }

    private fun updateDateInView() {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        mBinding.e4.setText(dateFormat.format(calendar.time))
    }
    override fun initData() {

        mBinding.b1.setOnClickListener({
            val body = AddMerchantBody(
                applicationArea = 120,
                applicationNumber = mBinding.e1.text.toString(),
                contactMethod = mBinding.e3.text.toString().toLong(),
                contactName = mBinding.e2.text.toString(),
                enterpriseName = "1",
                enterpriseProfileId = mBinding.e5.text.toString().toInt(),
                entryPersonnel = mBinding.e6.text.toString().toInt(),
                entryTime = mBinding.e4.text.toString(),
                id = 1,
                status = "新建",
                documents = mBinding.flowImage.list
            )

            mViewModel.addMerchant(body)
        })

    }

    override fun initListener() {
        mViewModel._mAddMerchantState.observeWithLifecycle(this){
            when(it){
                Empty ->    {}
                is Failed -> {}
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"申请成功")
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                FlowLayoutWithAddView.REQUEST_CAMERA -> {
                    data?.data?.let { uri -> mBinding.flowImage.addImage(uri) }
                }
                FlowLayoutWithAddView.REQUEST_GALLERY -> {
                    data?.data?.let { uri -> mBinding.flowImage.addImage(uri) }
                }
            }
        }
    }
}